import axios from 'axios'
import router from '@/router/routers'
import {
  Notification,
  Message
} from 'element-ui'
import store from '../store'
import {
  getToken
} from '@/utils/auth'
import Config from '@/settings'
import Cookies from 'js-cookie'

// 创建axios实例
console.log(process.env.NODE_ENV,'requset.js')
const service = axios.create({
  baseURL: process.env.NODE_ENV === 'development' ? process.env.VUE_APP_BASE_API+'admin/' : '/admin/', // api 的 base_url
  // baseURL: 'http://115.236.17.123:31624/' + 'admin/', // api 的 base_url
  //baseURL: process.env.NODE_ENV==='production'?'/':"http://100.0.6.38:80/" + 'admin/', // api 的 base_url
  headers: {
    // 禁止ie浏览器 get缓存 只请求一次
    'Cache-Control': 'no-cache',
    'Pragma': 'no-cache',
  },
  timeout: Config.timeout, // 请求超时时间
})
var msgShow = '',
  errShow = ''
// request拦截器
service.interceptors.request.use(
  config => {
    config.headers['Accept-Language'] = store.state.settings.language == 'en-US' ? 'en' :store.state.settings.language == 'zh-TW'?'zh-HK': store.state.settings.language
    // config.url = config.url + "?lang=" + (store.state.settings.language == 'en-US' ? 'en' : store.state.settings.language)
    if (getToken()) {
      config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
    }
    config.headers['Content-Type'] = 'application/json'
    msgShow = config.msg || false
    errShow = config.err || false;
    return config
  },
  error => {
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
  }
)

// response 拦截器
service.interceptors.response.use(
  response => {
    const code = response.status
    if (code < 200 || code > 300) {
      Notification.error({
        title: response.message
      })
      return Promise.reject('error')
    } else {
      if (msgShow) {
        Message({
          showClose: true,
          message: response.data.message,
          type: response.data.code == 10000 ? 'success' : 'warning'
        })
      } else if (!msgShow && errShow && response.data.code !== 10000) {
        Message({
          showClose: true,
          message: response.data.message,
          type: 'warning'
        })
      }
      return response.data
    }
  },
  error => {
    let code = 0
    try {
      code = error.response.data.status
    } catch (e) {
      if (error.toString().indexOf('Error: timeout') !== -1) {
        Notification.error({
          title: '网络请求超时',
          duration: 5000
        })
        return Promise.reject(error)
      }
    }
    if (code) {
      if (code === 10001) {
        store.dispatch('LogOut').then(() => {
          // 用户登录界面提示
          Cookies.set('point', 401)
          location.reload()
        })
      } else if (code === 403) {
        router.push({
          path: '/401'
        })
      } else {
        const errorMsg = error.response.data.message
        if (errorMsg !== undefined) {
          Notification.error({
            title: errorMsg,
            duration: 5000
          })
        }
      }
    } else {
      Notification.error({
        title: '接口请求失败',
        duration: 5000
      })
    }
    return Promise.reject(error)
  }
)
export default service
